﻿using DG.Tweening;
using Miao.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Miao.UI
{
    /// <summary>
    /// 从特定区域飞入并带有缩放反弹效果
    /// </summary>
    public class PullScaleUIMotion : BaseUIMotion
    {
        float pullDuration;      // 飞入持续时间
        float scaleDuration;     // 缩放效果持续时间
        float scaleMultiplier;   // 缩放倍数
        RectTransform.Edge edge;
        Vector3 fromPos, toPos;
        Vector3 originalScale;
        public PullScaleUIMotion(RectTransform.Edge edge, float pullDuration = 0.5f, float scaleDuration = 0.3f, float scaleMultiplier = 1.2f)
        {
            this.pullDuration = pullDuration;
            this.scaleDuration = scaleDuration;
            this.scaleMultiplier = scaleMultiplier;
            this.edge = edge;
        }
        public override void Init()
        {
            base.Init();
          
            fromPos = toPos = TweenGameObject.transform.localPosition;
            originalScale = TweenGameObject.transform.localScale;
        }

        public override void Play(bool forward, TweenCallback complete)
        {
            if (forward)
            {
                PlayForward(complete);
            }
            else
            {
                PlayBackward(complete);
            }
        }

        private void PlayForward(TweenCallback complete)
        {

            float width = Screen.width;
            float height = Screen.height;

            // 计算飞入的起始和结束位置
            switch (edge)
            {
                case RectTransform.Edge.Left:
                    fromPos.x = -width;
                    break;
                case RectTransform.Edge.Right:
                    fromPos.x = width;
                    break;
                case RectTransform.Edge.Top:
                    fromPos.y = height;
                    break;
                case RectTransform.Edge.Bottom:
                    fromPos.y = -height;
                    break;
            }

            // 设置初始位置和缩放
            TweenGameObject.transform.localPosition = fromPos;
            TweenGameObject.transform.localScale = originalScale;

            // 创建飞入动画序列
            Sequence sequence = DOTween.Sequence();

            // 第一步：飞入动画
            sequence.Append(TweenGameObject.transform.DOLocalMove(toPos, pullDuration)
                .SetEase(Ease.OutBack));

            // 第二步：放大动画
            sequence.Append(TweenGameObject.transform.DOScale(originalScale * scaleMultiplier, scaleDuration * 0.6f)
                .SetEase(Ease.OutQuad));

            // 第三步：缩回原大小
            sequence.Append(TweenGameObject.transform.DOScale(originalScale, scaleDuration * 0.4f)
                .SetEase(Ease.InOutQuad));

            // 完成回调
            sequence.OnComplete(complete);
        }

        private void PlayBackward(TweenCallback complete)
        {
            Vector3 fromPos, toPos;
            fromPos = toPos = TweenGameObject.transform.localPosition;
            Vector3 originalScale = TweenGameObject.transform.localScale;

            float width = Screen.width;
            float height = Screen.height;

            // 计算飞出的目标位置
            switch (edge)
            {
                case RectTransform.Edge.Left:
                    toPos.x = -width;
                    break;
                case RectTransform.Edge.Right:
                    toPos.x = width;
                    break;
                case RectTransform.Edge.Top:
                    toPos.y = height;
                    break;
                case RectTransform.Edge.Bottom:
                    toPos.y = -height;
                    break;
            }

            // 创建飞出动画序列
            Sequence sequence = DOTween.Sequence();

            // 先稍微缩小一点
            sequence.Append(TweenGameObject.transform.DOScale(originalScale * 0.9f, scaleDuration * 0.3f)
                .SetEase(Ease.InQuad));

            // 然后飞出
            sequence.Append(TweenGameObject.transform.DOLocalMove(toPos, pullDuration)
                .SetEase(Ease.InBack));

            // 完成回调
            sequence.OnComplete(complete);
        }
    }
}